package com.colodoo.wvs.manager.bug.action;

import com.colodoo.framework.common.Contants;
import com.colodoo.framework.common.Result;
import com.colodoo.framework.easyui.Tree;
import com.colodoo.wvs.manager.bug.model.Bug;
import com.colodoo.wvs.manager.bug.model.BugVO;
import com.colodoo.wvs.manager.bug.service.BugService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author colodoo
 * @date 2019-4-14 19:29:45
 * @description
 */
@RestController
@RequestMapping(value = "/bug")
@CrossOrigin
public class BugAction {

    @Resource
    private BugService bugService;

    /**
     * 新增数据
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/save")
    public Result save(@RequestBody Bug model) {
        bugService.saveBug(model);
        return Result.ok();
    }

    /**
     * 删除数据
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/delete")
    public Result delete(@RequestBody Bug model) {
        bugService.deleteBug(model);
        return Result.ok();
    }

    /**
     * 更新数据
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/update")
    public Result update(@RequestBody Bug model) {
        Map<String, Object> rspMap = new HashMap<String, Object>();
        bugService.updateBug(model);
        return Result.ok();
    }

    /**
     * 根据id查找单条数据
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/queryById")
    public Map<String, Object> queryById(@RequestBody Bug model) {
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put("rows", bugService.queryById(model));
        return rspMap;
    }

    /**
     * 查找列表
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/query")
    public List<BugVO> query(@RequestBody BugVO model) {
        return bugService.query(model);
    }

    /**
     * 查找分页列表
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/queryPage")
    public Map<String, Object> queryPage(@RequestBody BugVO model) {
        Map<String, Object> rspMap = new HashMap<String, Object>();
        PageInfo<BugVO> info = bugService.query(model.getPage(), model);
        rspMap.put(Contants.TABLE_ROWS, info.getList());
        rspMap.put(Contants.TABLE_TOTAL, info.getTotal());
        return rspMap;
    }

    /**
     * 查找树
     *
     * @param model
     * @return
     */
    @PostMapping(value = "/queryTree")
    public List<Tree> getTreeMenu(@RequestBody BugVO model) {
        return this.bugService.getTreeMenu(model.getParentBugId());
    }
}
